package com.yingma.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yingma.system.dao.JcSysOrganizationDao;
import com.yingma.system.entity.JcServerZone;
import com.yingma.system.dao.JcServerZoneDao;
import com.yingma.system.entity.JcSysOrganization;
import com.yingma.system.service.JcServerZoneService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (JcServerZone)表服务实现类
 *
 * @author makejava
 * @since 2020-03-18 15:15:47
 */
@Service("jcServerZoneService")
public class JcServerZoneServiceImpl implements JcServerZoneService {
    @Resource
    private JcServerZoneDao jcServerZoneDao;

    @Resource
    private JcSysOrganizationDao jcSysOrganizationDao;

    /**
     * 通过主键删除数据
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.jcServerZoneDao.deleteById(id) > 0;
    }
    /**
     *  获取服务区域信息
     * @param ZoneName
     * @param MuDiYunDian
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<JcServerZone> getInfo(String ZoneName, String MuDiYunDian,Integer pageNum, Integer pageSize) {

        QueryWrapper<JcServerZone> queryWrapper=new QueryWrapper<JcServerZone>();
        //查询前十条数据
        //queryWrapper.last("limit 20");
        if(!ZoneName.equals("") && !MuDiYunDian.equals("")){
            queryWrapper.like("jc_organization_id","SELECT ID FROM jc_sys_organization WHERE `NAME`='"+ZoneName+"'");
            queryWrapper.like("jc_zone_id","SELECT ID FROM jc_zone WHERE `NAME`='"+MuDiYunDian+"'");
        }else if(!ZoneName.equals("")){
            queryWrapper.inSql("jc_organization_id","SELECT ID FROM jc_sys_organization WHERE `NAME`='"+ZoneName+"'");
        }else if(!MuDiYunDian.equals("")){
            queryWrapper.inSql("jc_zone_id","SELECT ID FROM jc_zone WHERE `NAME`='"+MuDiYunDian+"'");
        }

        //Page<JcServerZone> pages=new Page<JcServerZone>(1,10);
        Page<JcServerZone> page=new Page<>(pageNum,pageSize,true);
        IPage<JcServerZone> iPage= jcServerZoneDao.selectPage(page,queryWrapper);
        System.out.println("总页数"+iPage.getPages());
        System.out.println("总记录数"+iPage.getTotal());
        List<JcServerZone> list=iPage.getRecords();
        System.out.println("记录条数==>"+list.size());
        //永远都给 list数组的第一个赋值
        if(list.size()>0){
            list.get(0).setTotalpageNum(new Long(iPage.getPages()).intValue());
            list.get(0).setTotalSize(new Long(iPage.getTotal()).intValue());
        }
        return list;
    }

    /**
     * 根据前台传的 组织区域名称查询返回ID
     * @return
     */
    public String getIdby(String str){
        QueryWrapper<JcSysOrganization> queryWrapper=new QueryWrapper<JcSysOrganization>();
        if(str.equals("")){
            //如果前台没有传值,则不查询,返回null
            List<JcSysOrganization> list= jcSysOrganizationDao.selectList(null);
            return null;
        }else {
            queryWrapper.like("name",str);
            //str 不为null时根据str(组织区域名称)查询,并返回ID
            JcSysOrganization jcSysOrganization= jcSysOrganizationDao.selectOne(queryWrapper);
            String id=jcSysOrganization.getId();
            return id;
        }
    }

    /**
     * 添加服务信息
     * @param jcServerZone
     * @return
     */
    @Override
    public int insert(JcServerZone jcServerZone) {
        return jcServerZoneDao.insert(jcServerZone);
    }


}
